/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;


import lombok.RequiredArgsConstructor;
import me.zhengjie.pojo.sqlServer.PPDeviceStatus;
import me.zhengjie.repository.PPDeviceStatusRepository;
import me.zhengjie.service.PPDeviceStatusService;
import me.zhengjie.service.dto.PPDeviceStatusDto;
import me.zhengjie.service.dto.PPDeviceStatusQueryCriteria;
import me.zhengjie.service.mapper.PPDeviceStatusMapper;
import me.zhengjie.utils.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-03
**/
@Service
@RequiredArgsConstructor
public class PPDeviceStatusServiceImpl implements PPDeviceStatusService {

    private final PPDeviceStatusRepository ppDeviceStatusRepository;
    private final PPDeviceStatusMapper ppDeviceStatusMapper;

    @Override
    public PageResult<PPDeviceStatusDto> queryAll(PPDeviceStatusQueryCriteria criteria, Pageable pageable){
        Page<PPDeviceStatus> page = ppDeviceStatusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppDeviceStatusMapper::toDto));
    }

    @Override
    public List<PPDeviceStatusDto> queryAll(PPDeviceStatusQueryCriteria criteria){
        return ppDeviceStatusMapper.toDto(ppDeviceStatusRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPDeviceStatusDto findById(Integer id) {
        PPDeviceStatus ppDeviceStatus = ppDeviceStatusRepository.findById(id).orElseGet(PPDeviceStatus::new);
        ValidationUtil.isNull(ppDeviceStatus.getId(),"PpDeviceStatus","id",id);
        return ppDeviceStatusMapper.toDto(ppDeviceStatus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPDeviceStatus resources) {
        ppDeviceStatusRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPDeviceStatus resources) {
        PPDeviceStatus ppDeviceStatus = ppDeviceStatusRepository.findById(resources.getId()).orElseGet(PPDeviceStatus::new);
        ValidationUtil.isNull( ppDeviceStatus.getId(),"PpDeviceStatus","id",resources.getId());
        ppDeviceStatus.copy(resources);
        ppDeviceStatusRepository.save(ppDeviceStatus);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppDeviceStatusRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPDeviceStatusDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPDeviceStatusDto ppDeviceStatus : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("码垛机编号", ppDeviceStatus.getCode());
            map.put("仓库编号(RTL，PP)", ppDeviceStatus.getWarehouseCode());
            map.put("设备类型", ppDeviceStatus.getEquipmentType());
            map.put("巷道号", ppDeviceStatus.getAisle());
            map.put("层号", ppDeviceStatus.getLayer());
            map.put("设备状态 (0-初始 1-正常 2-异常)", ppDeviceStatus.getStatus());
            map.put("设备状态变更时间", ppDeviceStatus.getLastupdated());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}